const positionTpl = require('../views/position.html');


const positionController = {
    async render({ res, router }) {
        //获取列表数据
        let listData = await this.getList().then(res => res);
        console.log(listData);


        //使用art-template 支持html
        let tpl = template.render(positionTpl, {
            data: JSON.parse(listData)
        });

        //页面渲染
        res.render(tpl);


        //点击按钮  路由的跳转 router.go（'/position/add')
        $('#jfh-add').on('click', () => {
            router.go('/position_add') //编程式导航
        });

        //进行删除的操作    
        this.removeItem(res, router);


    },
    getList() { //拿数据
        return $.ajax({
            url: "/api/position/find",
            success: function(res) {
                return res
            }
        });
    },
    removeItem(res, router) {
        let _this = this;
        $("tr[data-id]").on('click', '.jfh-remove', function() {
            let id = $(this).parents('tr').attr('data-id');
            let companyLogo = $(this).parents('tr').attr('data-img');
            $.ajax({
                type: "delete",
                url: "/api/position/remove",
                data: {
                    id,
                    companyLogo
                },
                success: function(response) {
                    _this.render({ res, router })
                    router.go('/position')
                    console.log('删除成功')
                }
            });
        })
    }
};

module.exports = positionController;